package com.ruoyi.bbsAdmin.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.bbsAdmin.domain.School;
import com.ruoyi.bbsAdmin.domain.vo.bbs_userVo;
import com.ruoyi.bbsAdmin.mapper.SchoolMapper;
import com.ruoyi.bbsAdmin.service.IUserSchoolService;
import com.ruoyi.bbsAdmin.service.RedisService;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.uuid.UUID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.bbsAdmin.mapper.bbs_userMapper;
import com.ruoyi.bbsAdmin.domain.bbs_user;
import com.ruoyi.bbsAdmin.service.Ibbs_userService;
import com.ruoyi.bbsAdmin.mapper.UserSchoolMapper;
import com.ruoyi.bbsAdmin.domain.UserSchool;
import com.ruoyi.bbsAdmin.domain.vo.multiUserStructure;
import com.ruoyi.bbsAdmin.domain.vo.updateUserStructure;
//updateUserStructure
import javax.annotation.Resource;

import static com.ruoyi.common.utils.PageUtils.startPage;

/**
 * 论坛用户Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-03-21
 */
@Service
public class bbs_userServiceImpl implements Ibbs_userService
{
    @Resource
    private bbs_userMapper bbs_userMapper;

    @Resource
    private UserSchoolMapper UserSchoolMapper;

    @Resource
    private SchoolMapper schoolMapper;
    @Autowired
    private IUserSchoolService userSchoolService;

    @Autowired
    RedisService redisService;

    /**
     * 查询论坛用户
     * 
     * @param userid 论坛用户主键
     * @return 论坛用户
     */
    @Override
    public bbs_user selectbbs_userByUserid(String userid)
    {
        return bbs_userMapper.selectbbs_userByUserid(userid);
    }


//    return cateMapper.selectCateList(cate);

    @Override
    public List<bbs_user> selectbbs_userListExport(Long schoolId,bbs_user bbs_user) {
        return bbs_userMapper.selectbbs_userList(bbs_user);
    }

    @Override
    public List<bbs_user> getUserListbyIds(String[] userids) {
        return bbs_userMapper.getUserListbyIds(userids);
    }

    @Override
    public List<bbs_user> selectAllSchoolUserList(multiUserStructure multiUserStructure) {
        List<bbs_user> allSchoolUserList= new ArrayList<>();
        allSchoolUserList = bbs_userMapper.getAllSchoolUserVo(multiUserStructure);
        return allSchoolUserList;


    }

    /**
     * 查询论坛用户列表
     * 
     * @param multiUserStructure 论坛用户
     * @return 论坛用户
     */
    @Override
    public List<bbs_user> selectbbs_userList(multiUserStructure multiUserStructure)
    {
        Long schoolId = multiUserStructure.getSchoolid();
//        Integer userState = multiUserStructure.getUserstate();
//        QueryWrapper<UserSchool> userSchoolQueryWrapper = new QueryWrapper<>();
        UserSchool userSchool = new UserSchool();
        if(schoolId!=null){
//           userSchoolQueryWrapper.eq("schoolId",schoolId);
           userSchool.setSchoolid(schoolId);
        }
        if(multiUserStructure.getUserstate()!=null){
            userSchool.setUserstate(multiUserStructure.getUserstate());
        }if(multiUserStructure.getUsertype()!=null){
             userSchool.setUsertype(multiUserStructure.getUsertype());
        }
        List<UserSchool> userSchoolList = userSchoolService.selectUserSchoolListByRole(userSchool);
        List<String> list = new ArrayList<String>();
        for (UserSchool item:userSchoolList){
            list.add(item.getUserid());
        }
//        System.out.println(list);
        if(list.size()==0){
            return null;
        }else if(multiUserStructure.getUserid()!=null && multiUserStructure.getUserid()!=""){ //查询参数中存在userid
            QueryWrapper<bbs_user> bbs_userQueryWrapper = new QueryWrapper<>();
            bbs_userQueryWrapper.eq("userId",multiUserStructure.getUserid());
            startPage();
            List<bbs_user> userList1 = bbs_userMapper.selectList(bbs_userQueryWrapper);
            //更新校园信息
            QueryWrapper<UserSchool> UserSchoolQueryWrapper = new QueryWrapper<>();
            UserSchoolQueryWrapper.eq("userId",multiUserStructure.getUserid());
            UserSchoolQueryWrapper.eq("schoolId",multiUserStructure.getSchoolid());
            UserSchoolQueryWrapper.orderByAsc("createTime");
//            UserSchool records = UserSchoolMapper.selectOne(UserSchoolQueryWrapper);
            List<UserSchool>  recordList = UserSchoolMapper.selectList(UserSchoolQueryWrapper);
            UserSchool records = new UserSchool();
            if (recordList.size()>0){
                records = recordList.get(0);
            }
            if (records!=null){
                userList1.get(0).setUserstate(records.getUserstate());
                userList1.get(0).setUsertype(records.getUsertype());
                userList1.get(0).setIdcardurl(records.getIdcardurl());
                //null处理，否则elementUI报错
                if(null == userList1.get(0).getIdcardurl())  userList1.get(0).setIdcardurl("");
            }
            return userList1;
        }
        //查询参数中带有昵称
        else if(multiUserStructure.getNickname()!=null){
            QueryWrapper<bbs_user> bbs_userQueryWrapper = new QueryWrapper<>();
            bbs_userQueryWrapper.like("nickName",multiUserStructure.getNickname());
            startPage();
            List<bbs_user> userList1 = bbs_userMapper.selectList(bbs_userQueryWrapper);
            //更新校园信息
            for (bbs_user item:userList1){
                QueryWrapper<UserSchool> UserSchoolQueryWrapper = new QueryWrapper<>();
                UserSchoolQueryWrapper.eq("userId",item.getUserid() );
                UserSchoolQueryWrapper.eq("schoolId",multiUserStructure.getSchoolid());
                UserSchoolQueryWrapper.orderByAsc("createTime");
//                UserSchool records = UserSchoolMapper.selectOne(UserSchoolQueryWrapper);
                List<UserSchool>  recordList = UserSchoolMapper.selectList(UserSchoolQueryWrapper);
                UserSchool records = new UserSchool();
                if (recordList.size()>0){
                    records = recordList.get(0);
                }
                if (records!=null){
                    item.setUserstate(records.getUserstate());
                    item.setUsertype(records.getUsertype());
                    item.setIdcardurl(records.getIdcardurl());
                    //null处理为空
                    if(null == item.getIdcardurl())  item.setIdcardurl("");
                    item.setStunum(records.getStunum());
                }
            }
            return userList1;
        } else {
            List <String> ids = new ArrayList<String>();
            QueryWrapper<bbs_user> bbs_userQueryWrapper = new QueryWrapper<>();
            for(String item:list){
                ids.add(item);
                bbs_userQueryWrapper = bbs_userQueryWrapper.eq("userId",item).or();
            }
            startPage();
            //List<bbs_user> userList1 = bbs_userMapper.selectList(bbs_userQueryWrapper);
            String[] idsArray = ids.toArray(new String[ids.size()]);
            List<bbs_user> userList1 = bbs_userMapper.getUserListbyIds(idsArray);
            for (bbs_user item:userList1){
                QueryWrapper<UserSchool> UserSchoolQueryWrapper = new QueryWrapper<>();
                UserSchoolQueryWrapper.eq("userId",item.getUserid() );
                UserSchoolQueryWrapper.eq("schoolId",multiUserStructure.getSchoolid());
                UserSchoolQueryWrapper.orderByDesc("updateTime");
//                UserSchool records = UserSchoolMapper.selectOne(UserSchoolQueryWrapper);
                List<UserSchool>  recordList = UserSchoolMapper.selectList(UserSchoolQueryWrapper);
                UserSchool records = new UserSchool();
                if (recordList.size()>0){
                    records = recordList.get(0);
                }
                if (records!=null){
                    item.setUserstate(records.getUserstate());
                    item.setUsertype(records.getUsertype());
                    item.setIdcardurl(records.getIdcardurl());
                    //null处理为空
                    if(null == item.getIdcardurl())  item.setIdcardurl("");
                    item.setStunum(records.getStunum());
                }
            }
            return userList1;
        }

    }

    /**
     * 查询论坛机器人用户列表
     *
     * @param bbs_userVo 论坛用户
     * @return 论坛用户
     */
    @Override
    public List<bbs_userVo> selectbbs_robotList(bbs_userVo bbs_userVo)
    {
        return bbs_userMapper.selectbbs_robotList(bbs_userVo);
    }


    /**
     * 新增论坛用户
     * 
     * @param Bbs_userVo 论坛用户
     * @return 结果
     */
    @Override
    public int insertbbs_user(bbs_userVo Bbs_userVo)
    {
        bbs_user Bbs_user = new bbs_user();
        String newUUID = UUID.randomUUID().toString().replace("-","");
        Bbs_user.setUserid(newUUID);
        Bbs_user.setNickname(Bbs_userVo.getNickname());
        Bbs_user.setHeadimgurl(Bbs_userVo.getHeadimgurl());
        Bbs_user.setOpenid(Bbs_userVo.getOpenid());
        Bbs_user.setPhone(Bbs_userVo.getPhone());
        Bbs_user.setInviter(SecurityUtils.getUsername());
        Bbs_user.setSex(0);
        Bbs_user.setUsertype(0);
        Bbs_user.setUserstate(0);
        Bbs_user.setCreatetime(new Date());
        Bbs_user.setUpdatetime(new Date());


        UserSchool user_school =new UserSchool();
        user_school.setId(UUID.randomUUID().toString().replace("-",""));
        user_school.setUserid(newUUID);
        user_school.setSchoolid((Bbs_userVo.getSchoolid()).longValue() );
        user_school.setUsertype(0);
        user_school.setUserstate(2);
        user_school.setCreatetime(new Date());
        user_school.setUpdatetime(new Date());
        UserSchoolMapper.insertUserSchool(user_school);
        return bbs_userMapper.insertbbs_user(Bbs_user);
    }

    /**
     * 修改论坛用户
     * 
     * @param bbs_user 论坛用户
     * @return 结果
     */

//    multiUserStructure

    @Override
    public int updatebbs_user(bbs_user bbs_user)
    {
        return bbs_userMapper.updatebbs_user(bbs_user);
    }

    @Override
    public int updateUser(updateUserStructure updateUserStructure) {
        String userId = updateUserStructure.getUserid();
        Integer userState = updateUserStructure.getUserstate();
        Integer userType = updateUserStructure.getUsertype();
        Long schoolId = updateUserStructure.getSchoolid();
//        根据userId
        UserSchool userSchool = new UserSchool();
        userSchool.setUsertype(userType);
        userSchool.setUserstate(userState);


        QueryWrapper<UserSchool> userSchoolQueryWrapper = new QueryWrapper<>();
        if(userId!=null){
            userSchoolQueryWrapper.eq("userid",userId);
            userSchoolQueryWrapper.eq("schoolid",schoolId);
        }
        UserSchool userSchoolUpdate = UserSchoolMapper.selectOne(userSchoolQueryWrapper);
        userSchoolUpdate.setUserstate(userState);
        userSchoolUpdate.setUsertype(userType);
        Integer res = UserSchoolMapper.updateById(userSchoolUpdate);
        //修改redis中的userSchool信息
        int redisRes = redisService.updateUserSchoolInfoInRedis(userSchoolUpdate);
        return res;
    }

    //添加一个多表更新的东西


    //添加一个混合搜索的东西



    /**
     * 批量删除论坛用户
     * 
     * @param userids 需要删除的论坛用户主键
     * @return 结果
     */
    @Override
    public int deletebbs_userByUserids(String[] userids)
    {
        return bbs_userMapper.deletebbs_userByUserids(userids);
    }

    /**
     * 删除论坛用户信息
     * 
     * @param userid 论坛用户主键
     * @return 结果
     */
    @Override
    public int deletebbs_userByUserid(String userid)
    {
        return bbs_userMapper.deletebbs_userByUserid(userid);
    }

    @Override
    public int judgebbs_userByUserid(String userid, Integer state,Long schoolid) {
        QueryWrapper<UserSchool> userSchoolQueryWrapper = new QueryWrapper<>();
        if(userid!=null){
            userSchoolQueryWrapper.eq("userid",userid);
            userSchoolQueryWrapper.eq("schoolid",schoolid);
            userSchoolQueryWrapper.orderByAsc("createTime");
        }
//        UserSchool userSchool = UserSchoolMapper.selectOne(userSchoolQueryWrapper);
        List<UserSchool>  recordList = UserSchoolMapper.selectList(userSchoolQueryWrapper);
//        UserSchool userSchool = new UserSchool();
        if (recordList.size()>0){
            Integer res =0;
            for (UserSchool userSchool: recordList) {
                userSchool.setUserstate(state);
                res = UserSchoolMapper.updateById(userSchool);
                //修改redis中的userSchool信息
                int redisRes = redisService.updateUserSchoolInfoInRedis(userSchool);
            }
            return res;
        }
        else{
            return 0;
        }


    }

//    @Override
//    public List<bbs_user> selectbbs_userListBySearch(Integer userState, Integer userType,Long schoolId) {
//
//        // 交叉查询
//        UserSchool userSchool = new UserSchool();
//        QueryWrapper<UserSchool> userSchoolQueryWrapper = new QueryWrapper<>();
//        if(schoolId!=null){
//            userSchool.setSchoolid(schoolId);
//        }if(userState!=null){
//            userSchool.setUserstate(userState);
//        }if(userType!=null){
//            userSchool.setUsertype(userType);
//        }
//        //先查到符合要求的用户的id，放入集合，然后遍历搜索bbs_user,组合成为新的结构体返回。
//        return null;
//    }
}
